#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "../inc/student.h"
#include "../inc/admin.h"
#include "../inc/general.h"

// 初始化管理员节点
Admin_Node_p admin_Init()
{
    Admin_Node_p admin_head = adminNode_Create();
    if (!admin_head)
        return NULL;

    // 初始化空链表：头节点自循环
    admin_head->next = admin_head;
    admin_head->prev = admin_head;

    Admin_t admin[] = {
        {"admin", "123", 1},
        {"admin1", "123", 1},
        {"admin2", "123", 1},
        {"admin3", "123", 1},
        {"admin4", "123", 1},
        {"teacher", "123", 0},
        {"tea", "123", 0},
        {"tea1", "123", 0},
        {"tea2", "123", 0}};

    int length = sizeof(admin) / sizeof(admin[0]);

    for (int i = 0; i < length; i++)
    {
        Admin_Node_p node = adminNode_Create();
        if (!node)
            continue;

        // 填充数据
        strcpy(node->data->username, admin[i].username);
        strcpy(node->data->password, admin[i].password);
        node->data->privilege = admin[i].privilege;
        // node->data->login_flag = admin[i].login_flag;

        // 将新节点插入到链表尾部
        Admin_Node_p tail = admin_head->prev; // 当前尾节点

        tail->next = node;
        node->prev = tail;

        node->next = admin_head; // 新节点指向头节点（循环）
        admin_head->prev = node; // 头节点指向新尾节点
    }

    return admin_head;
}

// 创建节点
Admin_Node_p adminNode_Create()
{
    Admin_Node_p p = (Admin_Node_p)malloc(sizeof(Admin_Node_t));
    p->data = (Admin_p)malloc(sizeof(Admin_t));
    p->next = p;
    p->prev = p;
    return p;
}

// 查找指定索引的节点
Admin_Node_p find_adminNode(Admin_Node_p admin_head, int index)
{
    Admin_Node_p p = admin_head->next;
    if (index < 0)
    {
        return NULL;
    }
    else if (index > 0)
    {
        if (p == admin_head)
        {
            return NULL;
        }
        for (int i = 0; i < index; i++)
        {
            p = p->next;
        }
    }
    return p;
}

// 通过账号名查找管理员节点
int getAdminIndex_ByUserName(Admin_Node_p admin_head, char *username)
{
    int index = 0;
    Admin_Node_p p = admin_head->next;
    while (p != admin_head)
    {
        if (strcmp(p->data->username, username) == 0)
        {
            return index;
        }
        p = p->next;
        index++;
    }
    return -1;
}

// 插入尾节点
void insertTail_adminNode(Admin_Node_p admin_head)
{
    if (!admin_head)
        return;

    Admin_Node_p newNode = adminNode_Create();
    if (!newNode)
        return;

    newNode->data = scanf_admin(admin_head);
    if (!newNode->data)
    {
        free(newNode);
        return;
    }
    Admin_Node_p tail = admin_head->prev;
    newNode->prev = tail;
    newNode->next = admin_head;
    tail->next = newNode;
    admin_head->prev = newNode;
}

// 获取账号数量
int getAdminCount(Admin_Node_p admin_head)
{
    int count = 0;
    Admin_Node_p p = admin_head->next;
    while (p != admin_head)
    {
        count++;
        p = p->next;
    }
    return count;
}

// 输入新的账号密码
Admin_p scanf_admin(Admin_Node_p admin_head)
{
    Admin_p p = (Admin_p)malloc(sizeof(Admin_t));
    char password[10];
    printf("请输入新建账号:");
    while (1)
    {
        scanf("%9s", p->username);
        if (getAdminIndex_ByUserName(admin_head, p->username) == -1)
        {
            break;
        }
        else
        {
            printf("账号已存在，请重新输入:");
        }
    }

    while (1)
    {
        printf("请输入密码:");
        scanf("%9s", p->password);
        printf("请再次输入密码:");
        scanf("%9s", password);
        if (strcmp(p->password, password) == 0)
        {
            break;
        }
        else
        {
            printf("两次密码不一致，请重新输入!\n");
        }
    }
    // p->login_flag = 0;
    p->privilege = 0;
    return p;
}
